package com.token.sql;
import java.sql.DriverManager;
import java.sql.Connection;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.View;
import java.util.Properties;
import org.logicalcobwebs.proxool.ProxoolFacade;
/**
 * 连接池处理
 * @author chenjianxiong 2020/2/16
 */
public class Pool {
    public Pool() {}
    /**
     * 判断池是否存在。
     * @param poolName 池的名称
     * @return boolean
     */
    public boolean isPoolExist(String poolName){
        boolean falg = false;
        for (int i = 0; i < ProxoolFacade.getAliases().length; i++) {
            if (ProxoolFacade.getAliases()[i].toLowerCase().equals(poolName.toLowerCase())) {
                falg = true;
                break;
            }
        }
        return falg;
    }

    /**
     * 获取池里的连接
     * @param arg0 名称
     * @return Connection
     * @throws Exception 错误抛出
     */
    public Connection getConnection(String arg0) throws Exception{
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
            return DriverManager.getConnection("proxool." + arg0.toUpperCase());
        } catch (Exception e) {
            System.out.println("---->proxool." + arg0.toUpperCase());
            throw e;
        }
    }

    /**
     * 根据连接池名称获取连接
     * @param initDb 集成数据库
     * @param arg1 pool name
     * @return Connection
     * @throws Exception 错误抛出
     */
    public Connection getConnectionByPoolName(Database initDb, String arg1) throws Exception{
        View view = null;
        Document doc = null;
        if (!isPoolExist(arg1)){
            view = initDb.getView("vw_fmLinkList_PoolName");
            view.setAutoUpdate(false);
            doc = view.getDocumentByKey(arg1, true);
            if (doc != null) {
                createPool(doc);
            }else{
                throw new Exception("没有找到连接池："+ arg1);
            }
        }
        if (doc != null) {doc.recycle();}
        if (view != null) {view.recycle();}
        return getConnection(arg1);
    }

    /**
     * 根据配置获取连接池的连接
     * @param initDb 集成数据库对象
     * @param sKey 配置名
     * @return Connection
     * @throws Exception 错误抛出
     */
    public Connection getConnectionByConfigName(Database initDb, String sKey) throws Exception{
        View view = initDb.getView("vw_fmLinkList_ConnectionName");
        view.setAutoUpdate(false);
        Document doc = view.getDocumentByKey(sKey, true);
        String arg1;
        if(doc != null){
            arg1 = doc.getItemValueString("PoolName");
        }else{
            throw new Exception("没有找到配置：" + sKey);
        }

        doc.recycle();
        view.recycle();
        return this.getConnectionByPoolName(initDb,arg1);
    }

    /**
     * 直接连接
     * @param doc 文档对象
     * @return Connection
     * @throws Exception 错误抛出
     */
    public static Connection createConn(Document doc)throws Exception{
        String driverName = doc.getItemValueString("driverClass");
        String dbURL = doc.getItemValueString("url");
        String userName = doc.getItemValueString("user");
        String userPwd = doc.getItemValueString("password");
        Class.forName(driverName);
        return DriverManager.getConnection(dbURL, userName, userPwd);
    }

    /**
     * 创建连接池
     * @param poolDoc 文档对象
     */
    public void createPool(Document poolDoc){
        try{
            Properties info = new Properties();
            String oracle = "Oracle";
            info.setProperty("user", poolDoc.getItemValueString("user"));
            info.setProperty("password", poolDoc.getItemValueString("password"));
            info.setProperty("proxool.test-before-use", "true");
            if(oracle.equalsIgnoreCase(poolDoc.getItemValueString("dbType"))){
                info.setProperty("proxool.house-keeping-test-sql", "select 1 from dual");
            }else{
                info.setProperty("proxool.house-keeping-test-sql", "select 1");
            }
            info.setProperty("proxool.minimun-connection-count", poolDoc.getItemValueString("setMinimunConnectionCount"));
            info.setProperty("proxool.maximum-connection-count", poolDoc.getItemValueString("setMaximumConnectionCount"));
            info.setProperty("proxool.maximum-connection-lifetime", poolDoc.getItemValueString("setMaxinmumConnectionLifetime"));
            info.setProperty("proxool.maximum-active-time", poolDoc.getItemValueString("setMaximumActivetime"));
            info.setProperty("proxool.simultaneous-build-throttle", poolDoc.getItemValueString("setSimultaneousBuildThrottle"));
            String alias = poolDoc.getItemValueString("PoolName").toUpperCase();
            String driverClass = poolDoc.getItemValueString("driverClass");
            String driverUrl = poolDoc.getItemValueString("url");
            String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl;
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
            String tempStr = ProxoolFacade.registerConnectionPool(url, info);
            if (tempStr.length() > 0) {
                System.out.println("创建连接池成功：---->" + tempStr);
            }else{
                System.out.println("创建连接池失败");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}